package com.zhangck.bos2_0.action.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.zhangck.bos2_0.Service.system.MenuService;
import com.zhangck.bos2_0.common.BaseAction;

import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.domain.system.User;

/** 
* @author 上午茶
* @version 创建时间：2018年6月17日 下午3:39:47 
* 类说明 
*/
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Actions
@Controller
public class MenuAction extends BaseAction<Menu> {
	
	@Autowired
	private MenuService menuService;
	
	@Action(value="menu_indexlist", results = { @Result(name = "success", type = "json") })
	public String menuAction(){
		
		List<Menu> list = menuService.findAll();
		
		ActionContext.getContext().getValueStack().push(list);
		return "success";
	}
	
	@Action(value="menu_indexadd", results = { @Result(name = "success", type = "redirect",location="/pages/system/menu.html") })
	public String menu_indexadd(){
		menuService.save(model);
		return "success";
	}
	
	
	private Integer newPage;
	public void setNewPage(Integer newPage) {
		this.newPage = newPage;
	}

	@Action(value = "menu_list", results = { @Result(name = "success", type = "json") })
	public String list() {
		// 调用业务层，查询所有菜单数据
//		List<Menu> menus = menuService.findAll();
//		System.out.println(menus);//懒加载
		// 存入值栈
//		ActionContext.getContext().getValueStack().push(menus);
		System.out.println(model.getPage());
		System.out.println(page);
		System.out.println(newPage);
		Pageable pageable = new PageRequest(newPage-1, rows);
		Page<Menu> pageData = menuService.findAll(pageable);
		pushPageDataValueStack(pageData);

		return SUCCESS;
	}
	
	
	@Action(value = "menu_list2", results = { @Result(name = "success", type = "json") })
	public String list2() {
		// 调用业务层，查询所有菜单数据
		List<Menu> menus = menuService.findAll();
		// 存入值栈
		ActionContext.getContext().getValueStack().push(menus);

		return SUCCESS;
	}

	@Action(value = "menu_save", results = { @Result(name = "success", type = "redirect", location = "/pages/system/menu.html") })
	public String save() {
		// 调用业务层 ，保存菜单数据
		menuService.save(model);
		return SUCCESS;
	}
	
	@Action(value = "menu_save2", results = { @Result(name = "success", type = "json") })
	public String save2() {
		// 调用业务层 ，保存菜单数据
		Map<String, Object> result = new HashMap<>();
		try {
			menuService.save(model);
			System.out.println(model);
			result.put("status", true);
			result.put("msg", "保存成功");
			result.put("data", model);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", true);
			result.put("msg", "保存成功:"+e.getMessage());
		}
		
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
	
	
	@Action(value = "menu_delete", results = { @Result(name = "success", type = "json") })
	public String delete() {
		// 调用业务层 ，保存菜单数据
		Map<String, Object> result = new HashMap<>();
		try {
			menuService.delete(model);
			result.put("status", true);
			result.put("msg", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", true);
			result.put("msg", "保存成功:"+e.getMessage());
		}
		
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}

	@Action(value = "menu_showmenu", results = {@Result(name = "success", type = "json") })
	public String showMenu() {
		// 调用业务层，查询当前用户具有菜单 列表
		Subject subject = SecurityUtils.getSubject();
		User user = (User) subject.getPrincipal();
System.out.println(111111111);
		List<Menu> menus = menuService.findByUser(user);
		ActionContext.getContext().getValueStack().push(menus);

		return SUCCESS;
	}
	
	public static void main(String[] args) throws Exception {
		Menu m1 = new Menu();
		m1.setId(1);m1.setName("aaa");
		Menu parent = new Menu();
		parent.setId(0);
//		System.out.println(JSONObject.toJSONString(m1));//fastjson获取不到不标准的getter方法
		System.out.println(JSONUtil.serialize(m1, null, null, false, true)); //struts2-json插件他实现json序列化的时候 有点特别
	}
	
}


